<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Object_usage extends CI_Controller {

	function __construct() {
		parent::__construct();
	}

	public function index($sequence = "") {
		$html = $this -> init("基礎資料-物件用途", "jquery,pagination,base_data", "table");
		$this -> load -> model('object_usage_model');
		$keyword = "";
		$config['per_page'] = 10;
		$config['total_rows'] = $this -> object_usage_model -> get_all_count();
		$config['url'] = '/object_usage/page_ajax';
		$config['num_pages'] = ceil($config['total_rows'] / $config['per_page']);
		$limit = $config['per_page'] . ",0";
		$html['page'] = $config;
		$html['sequence'] = $sequence;
		$html["object_usage"] = $this -> object_usage_model -> query_keyword($keyword, $limit);
		$this -> load -> view('object_usage', $html);
	}

	public function page_ajax() {
		$this -> load -> model('object_usage_model');
		$page = $this -> input -> post('page');
		$sequence = $this -> input -> post('sequence');
		$keyword = "";
		$html["edit_mode"] = "";
		$config['per_page'] = 10;
		$p = $config['per_page'] * ($page - 1);
		$limit = $config['per_page'] . "," . $p;
		$html["result"] = $this -> object_usage_model -> query_keyword($keyword, $limit);
		$this -> load -> view('object_usage_ajax', $html);
	}

	public function add() {
		$this -> load -> model('object_usage_model');
		$data['value'] = $this -> input -> post('value');
		$data['explanation'] = $this -> input -> post('explanation');
		$data['sequence'] = $this -> input -> post('sequence');
		$return_result = $this -> object_usage_model -> add($data);
		if ($return_result > 0) {
			redirect("/object_usage");
		} else {
			echo "插入失敗";
		}
	}

	public function edit_ajax() {
		$this -> load -> model('object_usage_model');
		$id = $this -> input -> post('object_usage_id');
		$html["edit_mode"] = "edit";
		$html["info"] = $this -> object_usage_model -> get_info($id);
		$this -> load -> view('object_usage_ajax', $html);
	}

	public function edit_submit_ajax() {
		$this -> load -> model('object_usage_model');
		$data['id'] = $this -> input -> post('object_usage_id');
		$data['value'] = $this -> input -> post('value');
		$data['explanation'] = $this -> input -> post('explanation');
		$data['sequence'] = $this -> input -> post('sequence');
		$data['mtime'] = time();
		$return_result = $this -> object_usage_model -> update_data($data);
		if ($return_result) {
			$html["edit_mode"] = "edit_return";
			$html["info"] = $this -> object_usage_model -> get_info($data['id']);
			$this -> load -> view('object_usage_ajax', $html);
		} else {
			echo "0";
		}
	}

	public function cancel_edit_ajax() {
		$this -> load -> model('object_usage_model');
		$id = $this -> input -> post('object_usage_id');
		$html["edit_mode"] = "edit_return";
		$html["info"] = $this -> object_usage_model -> get_info($id);
		$this -> load -> view('object_usage_ajax', $html);
	}

	public function data_delete($id = null) {
		$this -> load -> model('object_usage_model');
		if ($id != null) {
			$data["id"] = $id;
			$return_result = $this -> object_usage_model -> del($data);
			if ($return_result) {
				redirect("/object_usage");
			} else {
				echo "刪除失敗";
			}
		} else {
			echo "刪除失敗";
		}
	}

	public function object_usage_josn() {

		$this -> load -> model('object_usage_model');
		$query = trim($this -> input -> get('query'));

		$market_state = $this -> object_usage_model -> get_not_in2($query);
		echo urldecode(json_encode($market_state));
	}


	public function market_usage_josn() {

		$this -> load -> model('market_model');
		$query = trim($this -> input -> get('query'));

		$market_state = $this -> market_model -> get_not_in2($query);
		echo urldecode(json_encode($market_state));
	}

	public function get_market_case_name_josn() {
        $this->load->model('market_model');
        $query = trim($this->input->get('query'));
        $object_case_name = $this->market_model->get_market_case_name($query);
        echo urldecode(json_encode($object_case_name));
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
